col4 bit P2.7
col3 bit P2.6
col2 bit P2.5
col1 bit P2.4
row1 bit P2.3
row2 bit P2.2
row3 bit P2.1
row4 bit P2.0
keydata equ 70h
keybounc equ 71h
keyport equ P2
ORG 0h
	MOV P2,#11111111b
	CLR P3.6
START: 	CALL keypad4x4 ;CALLing subroutine keypad4x4
	MOV A,keydata ;A = keydata
	CJNE A,#0FFh,WrLCD;
	SJMP start ;LOOPING FOREVER PART 1
;
WrLCD: 	MOV R1,#80h ;Pick DDRAM 1st row and 1st col
	CALL write_inst
	MOV R1,#30h
	ADD A,R1
	MOV R1,A
	CALL write_data ;write data
	SJMP start ;LOOPING FOREVER PART 2;
;
Init_lcd:
	MOV r1,#00000001b ;Display clear
	CALL write_inst
	MOV r1,#00111000b ;Function set,Data 8 bit,2 line font 5x7
	CALL write_inst
	MOV r1,#00001100b ;Display on,cursor off,cursor blink off
	CALL write_inst
	MOV r1,#00000110b ;Entry mode,Set increment
	CALL write_inst
RET
;
Write_inst:
	CLR P3.5 ;P3.5 = RS =0
	MOV P0,R1 ;copy R1 ke P0
	SETB P3.7 ;P3.7 =EN = 1
	CALL delay ;CALL delay time
	CLR P3.7 ;P3.7 =EN = 0
RET
;
Write_data:
	SETB P3.5 ;P3.5 = RS =1
	MOV P0,R1 ;copy R1 ke P0
	SETB P3.7 ;P3.7 =EN = 1
	CALL delay ;CALL delay time
	CLR p3.7 ;P3.7 =EN = 0
RET
;====================================
; subroutine scan keypad 4x4
;====================================
Keypad4x4:
	MOV keybounc,#50 ;keybounc = 50
	MOV keyport,#0FFh ;keyport=P2= FF
	CLR col4 ;col4 = 0
;
keyCOR: JB row4,keyMEN ; Key COR
	DJNZ keybounc,KeyCOR
	MOV keydata,#0Ah ;Data Output
RET
;
keyMEN: JB row3,keyUpA ; Key MEN
	DJNZ keybounc,keyMEN
	MOV keydata,#0bh ;Data Output
RET
keyUpA: JB row2,keyDnA ; Key Up Arrow
	DJNZ keybounc,keyUpA
	MOV keydata,#0ch ;Data Output
RET
;
keyDnA: JB row1,key3 ; Key Down Arrow
	DJNZ keybounc,keyDnA
	MOV keydata,#0dh ;Data Output
RET
;==========================================
key3: 	SETB col4
	CLR col3
	JB row4,key6
	DJNZ keybounc,key3 ; Key 3
	MOV keydata,#03h ;Data Output
RET
;
key6: 	JB row3,key9
	DJNZ keybounc,key6 ; Key 6
	MOV keydata,#06h ;Data Output
RET
;
key9: 	JB row2,keyENT
	DJNZ keybounc,key9 ; Key 9
	MOV keydata,#09h ;Data Output
RET
;
keyENT: JB row1,key2
	DJNZ keybounc,keyENT ; Key ENT
	MOV keydata,#0FFh
	CALL init_lcd
RET
;============================================
key2: 	SETB col3
	CLR col2
	JB row4,key5
	DJNZ keybounc,key2
	MOV keydata,#02h ;Data Output
RET
;
key5: 	JB row3,key8
	DJNZ keybounc,key5
	MOV keydata,#05h ; Data Output
RET
;
key8: 	JB row2,key0
	DJNZ keybounc,key8
	MOV keydata,#08h ;Data Output
RET
;
key0: 	JB row1,key1
	DJNZ keybounc,key0
	MOV keydata,#00h ;Data Output
RET
;==============================================
key1: 	SETB col2
	CLR col1
	JB row4,key4
	DJNZ keybounc,key1
	MOV keydata,#01h ;Data Output
RET
;
key4: 	JB row3,key7
	DJNZ keybounc,key4
	MOV keydata,#04h ;Data Output
RET
;
key7: 	JB row2,keyCAN
	DJNZ keybounc,key7
	MOV keydata,#07h ;Data Output
RET
;
keyCAN: JB row1,Nokey
	DJNZ keybounc,keyCAN
	MOV keydata,#0Fh ;Data Output
RET
;
Nokey: MOV keydata,#0FFh
RET
;================================
;The end of Keypad 4x4 subroutine
;================================
delay: 	mov R0,#01h 
delay1: mov R7,#0fh
djnz R0,delay1
RET

DataBase36:
	db '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
END